package com.freewater.springboot.demo.ioc.lifecycle.smartlifecycle;

import com.freewater.springboot.demo.PackageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.support.GenericApplicationContext;

import java.util.concurrent.TimeUnit;

@Slf4j
public class Main {

    public static void main(String[] args) throws InterruptedException {
        GenericApplicationContext applicationContext = new GenericApplicationContext();
        final String xmlPath = PackageUtils.getDefaultAppContextXmlPath(Main.class);
        new XmlBeanDefinitionReader(applicationContext).loadBeanDefinitions(xmlPath);
        applicationContext.refresh();
        log.info("使用ApplicationContext：{}", xmlPath);
        // 不需要手动start，默认SmartLifeCycle会跟随ApplicationContext的refresh启动
        applicationContext.registerShutdownHook(); // 注册jvm关闭回调，jvm关闭时一并关闭spring容器
        //applicationContext.stop();
        TimeUnit.SECONDS.sleep(1); // 1秒后关闭jvm虚拟机，模拟手动关闭程序
        System.exit(0);
    }
}
